package com.quanxiaoha.weblog.web.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quanxiaoha.weblog.common.domain.dos.CommentDO;
import com.quanxiaoha.weblog.common.domain.mapper.CommentMapper;
import com.quanxiaoha.weblog.web.dao.CommentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentDaoImpl implements CommentDao {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public int insert(CommentDO commentDO) {
        return commentMapper.insert(commentDO);
    }

    @Override
    public List<CommentDO> selectByArticleId(Long articleId) {
        LambdaQueryWrapper<CommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentDO::getArticleId, articleId)
                .eq(CommentDO::getStatus, 1)  // 只查询正常状态的评论
                .eq(CommentDO::getIsChecked, 1) // 只查询已审核通过的评论
                .orderByDesc(CommentDO::getCreateTime);
        return commentMapper.selectList(wrapper);
    }

    @Override
    public CommentDO selectById(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public int updateStatusById(Long id, Integer status) {
        CommentDO commentDO = CommentDO.builder()
                .id(id)
                .status(status)
                .build();
        return commentMapper.updateById(commentDO);
    }

    @Override
    public Long selectCountByArticleId(Long articleId) {
        LambdaQueryWrapper<CommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentDO::getArticleId, articleId)
                .eq(CommentDO::getStatus, 1)
                .eq(CommentDO::getIsChecked, 1); // 只统计已审核通过的评论
        return commentMapper.selectCount(wrapper);
    }
}